package cn.lirenyi.pet.service.impl;

import cn.lirenyi.basic.service.impl.BaseServiceImpl;
import cn.lirenyi.basic.util.CodeGenerateUtils;
import cn.lirenyi.basic.util.DistanceUtil;
import cn.lirenyi.basic.util.PageList;
import cn.lirenyi.basic.util.Point;
import cn.lirenyi.order.domain.PetAcquisitionOrder;
import cn.lirenyi.order.mapper.PetAcquisitionOrderMapper;
import cn.lirenyi.org.domain.Employee;
import cn.lirenyi.org.domain.Shop;
import cn.lirenyi.org.mapper.EmployeeMapper;
import cn.lirenyi.org.mapper.ShopMapper;
import cn.lirenyi.pet.domain.Pet;
import cn.lirenyi.pet.domain.SearchMasterMsg;
import cn.lirenyi.pet.mapper.SearchMasterMsgMapper;
import cn.lirenyi.pet.query.SearchMasterMsgQuery;
import cn.lirenyi.pet.service.IPetService;
import cn.lirenyi.pet.service.ISearchMasterMsgService;
import cn.lirenyi.user.domain.User;
import cn.lirenyi.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private IPetService iPetService;

    @Autowired
    private PetAcquisitionOrderMapper petAcquisitionOrderMapper;

    @Override
    public void publish(SearchMasterMsg searchMasterMsg,Long loginInfoId) {

        // 获取登录人信息
        User user = userMapper.queryLoginInfoId(loginInfoId);
        // 设置登录人id
        searchMasterMsg.setUser_id(user.getId());
        // 设置登录人
        searchMasterMsg.setUser(user);

        // 获取距离最近的店铺（通过经纬度计算）
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        Shop nearestShop = DistanceUtil.getNearestShop(point, shopMapper.queryAll());
        searchMasterMsg.setShop(nearestShop);
        searchMasterMsg.setShop_id(nearestShop.getId());
        // 保存寻主的信息
        searchMasterMsgMapper.add(searchMasterMsg);
    }

    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery query, Long id) {

        Employee employee = employeeMapper.queryLoginInfoId(id);
        if(employee.getShop_id() != null){
            query.setShopId(employee.getShop_id());
        }
        query.setState(0);
        return super.queryPage(query);
    }

    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery query, Long id) {

        Employee employee = employeeMapper.queryLoginInfoId(id);
        if(employee.getShop_id() != null){
            query.setShopId(employee.getShop_id());
        }
        query.setState(1);
        return super.queryPage(query);
    }

    @Override
    public PageList<SearchMasterMsg> queryUserMsg(SearchMasterMsgQuery query, Long id) {

        User user = userMapper.queryLoginInfoId(id);
        query.setUserId(user.getId());
        return super.queryPage(query);
    }

    @Override
    public void handle(Pet pet, Long id) {

        // 修改消息状态
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.queryById(pet.getSearch_master_msg_id());
        searchMasterMsg.setState(1);
        searchMasterMsgMapper.update(searchMasterMsg);

        // 生成宠物信息
        iPetService.add(pet);

        //  生成订单
        SearchMasterMsg masterMsg = searchMasterMsgMapper.queryById(pet.getSearch_master_msg_id());
        Employee employee = employeeMapper.queryLoginInfoId(id);
        PetAcquisitionOrder acquisitionOrder = pet2order(pet, masterMsg, employee.getId());
        petAcquisitionOrderMapper.add(acquisitionOrder);

        // 支付订单
        if(acquisitionOrder.getPrice().longValue() > 0){
            System.out.println("下单成功！支付金额是："+acquisitionOrder.getPrice());
        }

    }


    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg adopt, Long empId) {
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
        order.setState(0);//待支付
        order.setPrice(pet.getCostprice());
        order.setAddress(adopt.getAddress());
        String orderSn = CodeGenerateUtils.generateOrderSn(adopt.getUser_id());
        order.setOrderSn(orderSn);
        order.setPet_id(pet.getId());
        order.setUser_id(adopt.getUser_id());
        order.setPaytype(0);
        order.setShop_id(pet.getShop_id());
        order.setEmp_id(empId);
        return order;
    }
}
